import { createStore } from "vuex"
import api from "@/api"

export default createStore({
  state: {
    // 通知状态
    notification: {
      show: false,
      message: "",
    },

    // 仪表盘数据
    dashboardStats: null,
    recentActivities: [],

    // 需求优化数据
    optimizedRequirements: null,

    // 测试用例数据
    testCases: null,

    // 加载状态
    loading: {
      dashboard: false,
      requirements: false,
      testCases: false,
    },
  },

  mutations: {
    // 通知相关
    SHOW_NOTIFICATION(state, message) {
      state.notification = {
        show: true,
        message,
      }
    },
    HIDE_NOTIFICATION(state) {
      state.notification.show = false
    },

    // 仪表盘相关
    SET_DASHBOARD_STATS(state, stats) {
      state.dashboardStats = stats
    },
    SET_RECENT_ACTIVITIES(state, activities) {
      state.recentActivities = activities
    },

    // 需求优化相关
    SET_OPTIMIZED_REQUIREMENTS(state, data) {
      state.optimizedRequirements = data
    },

    // 测试用例相关
    SET_TEST_CASES(state, data) {
      state.testCases = data
    },

    // 加载状态
    SET_LOADING(state, { type, status }) {
      state.loading[type] = status
    },
  },

  actions: {
    // 通知相关
    showNotification({ commit, dispatch }, message) {
      commit("SHOW_NOTIFICATION", message)
      // 3秒后自动关闭
      setTimeout(() => {
        dispatch("closeNotification")
      }, 3000)
    },
    closeNotification({ commit }) {
      commit("HIDE_NOTIFICATION")
    },

    // 仪表盘相关
    async fetchDashboardStats({ commit }) {
      commit("SET_LOADING", { type: "dashboard", status: true })
      try {
        const response = await api.getDashboardStats()
        commit("SET_DASHBOARD_STATS", response.data)
      } catch (error) {
        console.error("获取仪表盘统计数据失败:", error)
      } finally {
        commit("SET_LOADING", { type: "dashboard", status: false })
      }
    },

    async fetchRecentActivities({ commit }) {
      try {
        const response = await api.getRecentActivities()
        commit("SET_RECENT_ACTIVITIES", response.data)
      } catch (error) {
        console.error("获取最近活动失败:", error)
      }
    },

    // 需求优化相关
    async optimizeRequirements({ commit, dispatch }, { content, title }) {
      commit("SET_LOADING", { type: "requirements", status: true })
      try {
        const response = await api.optimizeRequirements(content, title)
        commit("SET_OPTIMIZED_REQUIREMENTS", response.data.data)
        dispatch("showNotification", "需求优化完成")
      } catch (error) {
        console.error("需求优化失败:", error)
        dispatch("showNotification", "需求优化失败，请重试")
      } finally {
        commit("SET_LOADING", { type: "requirements", status: false })
      }
    },

    async optimizeRequirementsFromFile({ commit, dispatch }, { file, title }) {
      commit("SET_LOADING", { type: "requirements", status: true })
      try {
        const formData = new FormData()
        formData.append("file", file)
        formData.append("title", title)

        const response = await api.optimizeRequirementsFromFile(formData)
        commit("SET_OPTIMIZED_REQUIREMENTS", response.data.data)
        dispatch("showNotification", "文件解析与需求优化完成")
      } catch (error) {
        console.error("文件需求优化失败:", error)
        dispatch("showNotification", "文件需求优化失败，请重试")
      } finally {
        commit("SET_LOADING", { type: "requirements", status: false })
      }
    },

    // 测试用例相关
    async generateTestCases({ commit, dispatch }, { content, title }) {
      commit("SET_LOADING", { type: "testCases", status: true })
      try {
        const response = await api.generateTestCases(content, title)
        commit("SET_TEST_CASES", response.data.data)
        dispatch("showNotification", "测试用例生成完成")
      } catch (error) {
        console.error("测试用例生成失败:", error)
        dispatch("showNotification", "测试用例生成失败，请重试")
      } finally {
        commit("SET_LOADING", { type: "testCases", status: false })
      }
    },

    async generateTestCasesFromFile({ commit, dispatch }, { file, title }) {
      commit("SET_LOADING", { type: "testCases", status: true })
      try {
        const formData = new FormData()
        formData.append("file", file)
        formData.append("title", title)

        const response = await api.generateTestCasesFromFile(formData)
        commit("SET_TEST_CASES", response.data.data)
        dispatch("showNotification", "文件解析与测试用例生成完成")
      } catch (error) {
        console.error("文件测试用例生成失败:", error)
        dispatch("showNotification", "文件测试用例生成失败，请重试")
      } finally {
        commit("SET_LOADING", { type: "testCases", status: false })
      }
    },
  },
})